﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HQDBF;

namespace StockMonitor.Business
{
    /// <summary>
    /// DBF行情库访问类
    /// </summary>
    /// <remarks>
    /// 代码审查：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 审查时间：<datetime>2012-10-16 4:37 PM</datetime>
    /// </remarks>
    public class DBFPriceProvider : ICurrentPriceProvider
    {
        /// <summary>
        /// 数据访问对象
        /// </summary>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:40 PM</datetime>
        /// </remarks>
        PriceQuery query = new PriceQuery();

        /// <summary>
        /// 获得当前股票的价格
        /// </summary>
        /// <param name="stockCode">股票编号</param>
        /// <returns>
        /// 当前价格
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:40 PM</datetime>
        /// </remarks>
        public double GetCurrentPrice(string stockCode)
        {
            return query.GetBuyPriceX(stockCode, 0);
        }

        /// <summary>
        /// 获取当前买卖价格
        /// </summary>
        /// <param name="stockCode">股票代码</param>
        /// <param name="priceType">价格类型</param>
        /// <returns>
        /// 所需价格
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:40 PM</datetime>
        /// </remarks>
        public double GetPrice(string stockCode, Enums.PriceType priceType)
        {
            if (priceType > Enums.PriceType.未设置)
            {
                return query.GetBuyPriceX(stockCode, Convert.ToInt32(priceType));
            }
            else
            {
                return query.GetSellPriceX(stockCode, Convert.ToInt32(priceType) * -1);
            }
        }

        /// <summary>
        /// 批量获取股票的当前价格
        /// </summary>
        /// <param name="stockCodes">股票代码列表</param>
        /// <returns>
        /// 结果集
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:40 PM</datetime>
        /// </remarks>
        public List<StockPrice> BatchGetCurrentPrice(List<string> stockCodes)
        {
            if (stockCodes == null || stockCodes.Count == 0)
                return new List<StockPrice>();

            List<StockPrice> list = new List<StockPrice>();

            double[] prices = query.GetPricesX(stockCodes.ToArray(), 0, true);

            int i = 0;
            foreach (string code in stockCodes.OrderBy(s => s))
            {
                list.Add(new StockPrice() { StockCode = code, Price = prices[i] });
                i++;
            }

            return list;
        }
    }
}
